package com.ruoyi.laiwu.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.laiwu.domain.vo.LwAlarmExtendVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.laiwu.domain.bo.LwAlarmPositionBo;
import com.ruoyi.laiwu.domain.vo.LwAlarmPositionVo;
import com.ruoyi.laiwu.domain.LwAlarmPosition;
import com.ruoyi.laiwu.mapper.LwAlarmPositionMapper;
import com.ruoyi.laiwu.service.ILwAlarmPositionService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 告警位置Service业务层处理
 *
 * @author ruoyi
 * @date 2024-03-08
 */
@RequiredArgsConstructor
@Service
public class LwAlarmPositionServiceImpl implements ILwAlarmPositionService {

    private final LwAlarmPositionMapper baseMapper;

    /**
     * 查询告警位置
     */
    @Override
    public LwAlarmPositionVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询告警位置列表
     */
    @Override
    public TableDataInfo<LwAlarmPositionVo> queryPageList(LwAlarmPositionBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<LwAlarmPosition> lqw = buildQueryWrapper(bo);
        Page<LwAlarmPositionVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询告警位置列表
     */
    @Override
    public List<LwAlarmPositionVo> queryList(LwAlarmPositionBo bo) {
        LambdaQueryWrapper<LwAlarmPosition> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<LwAlarmPosition> buildQueryWrapper(LwAlarmPositionBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<LwAlarmPosition> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getSubId() != null, LwAlarmPosition::getSubId, bo.getSubId());
        lqw.eq(bo.getLongitude() != null, LwAlarmPosition::getLongitude, bo.getLongitude());
        lqw.eq(bo.getLatitude() != null, LwAlarmPosition::getLatitude, bo.getLatitude());
        lqw.eq(bo.getPan() != null, LwAlarmPosition::getPan, bo.getPan());
        lqw.eq(bo.getTilt() != null, LwAlarmPosition::getTilt, bo.getTilt());
        lqw.eq(bo.getZoom() != null, LwAlarmPosition::getZoom, bo.getZoom());
        lqw.eq(bo.getHeight() != null, LwAlarmPosition::getHeight, bo.getHeight());
        lqw.eq(bo.getMfrs() != null, LwAlarmPosition::getMfrs, bo.getMfrs());
        return lqw;
    }

    /**
     * 新增告警位置
     */
    @Override
    public Boolean insertByBo(LwAlarmPositionBo bo) {
        LwAlarmPosition add = BeanUtil.toBean(bo, LwAlarmPosition.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改告警位置
     */
    @Override
    public Boolean updateByBo(LwAlarmPositionBo bo) {
        LwAlarmPosition update = BeanUtil.toBean(bo, LwAlarmPosition.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(LwAlarmPosition entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除告警位置
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public LwAlarmPositionVo queryByAlarmId(Long subId) {
        LambdaQueryWrapper<LwAlarmPosition> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(LwAlarmPosition::getSubId,subId);
        List<LwAlarmPositionVo> lwAlarmPositionVos = baseMapper.selectVoList(wrapper);
        if(lwAlarmPositionVos.isEmpty()){
            return null;
        }
        return lwAlarmPositionVos.get(0);
    }
}
